package eu.reply.android.flashm;

import java.io.Console;
import java.util.ArrayList;

import eu.reply.android.flashm.entities.Event;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class EventListAdapter extends ArrayAdapter<Event> {
	private final Context context;
	private final ArrayList<Event> values;

	public EventListAdapter(Context context, ArrayList<Event> values) {
		super(context, R.layout.event_list_item, values);
		this.context = context;
		this.values = values;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		Log.d("EventListAdapter", "getView: " + position);
		LayoutInflater inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View rowView = inflater
				.inflate(R.layout.event_list_item, parent, false);

		// Set event name
		TextView textViewEventName = (TextView) rowView
				.findViewById(R.id.event_name);
		textViewEventName.setText(values.get(position).getName());

		// Set event date
		TextView textViewEventDate = (TextView) rowView
				.findViewById(R.id.event_date);
		textViewEventDate.setText(values.get(position).getDate().toString());

		// Set attendees
		TextView textViewAttendees = (TextView) rowView
				.findViewById(R.id.event_attending);
		Integer attendeesSize = values.get(position).getAttending().size();
		String attendeesText = attendeesSize > 0 ? attendeesSize.toString()
				: "No attendees yet";
		textViewAttendees.setText(attendeesText);

		// Set image
		((ImageView) rowView.findViewById(R.id.event_image))
				.setImageResource(values.get(position).getImageResourceId());

		Log.d("EventListAdapter", values.get(position).getName());

		return rowView;
	}
}
